var express = require('express');
var router = express.Router();
var User = require('../models/user_model.js').User;

/* GET home page. */


router.post('/register', function (req, res) {
    console.log('保存注册信息:' + JSON.stringify(req.body));
    User.create(req.body, function(err, user){
        if (err) {
            console.log('保存失败');
            res.json({code: 1, msg: '保存成功！', data : user});
        } else {
            res.json({code: 0, msg: '保存成功！', data : user});
        }
    });
});

router.post('/login', function (req, res) {
    User.findOne({email: req.body.email}, function (err, user) {
        console.log('user info:' + JSON.stringify(user));
        if (user == null) {
            res.json({code: 10001, msg: '用户不存在'});
        }
        if (user != null && user.password != req.body.password) {
            res.json({code: 10002, msg: '密码不正确'});
        }
        user.password = undefined;
        res.json({code: 0, msg: 'success', data: user});
    });
});

module.exports = router;
